 <template> 
  <div class="divbox">
      
    <draggable
			class="imgbox"
			tag="ul"
			v-model="fileUrl"
			@start="drag = true"
			@end="drag = false"
			@update="draggEnd"
	>
		<div class="imgboxInner" v-for="(item,index) in fileUrl" :key="index">
			<img :src="item"  width="148" height="148" >
      <i class="el-icon-search delIcon imgSearch" @click="searchImg(item,index)"></i>
			<i class="el-icon-delete delIcon imgDelete" @click="deleImg(item,index)"></i>
		</div>
	</draggable> 
    <el-upload
        :show-file-list="false"
      :action="action"
      :data="dataObj"
      list-type="picture-card"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
      :limit="maxCount"
      :on-exceed="handleExceed"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" width="40%" top="5vh">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
    import draggable from 'vuedraggable'
  import {policy} from '@/api/oss'
  import utils from '../../utils/utils'
  export default {
      components:{
          draggable
      },
    name: 'multiUploadDraggable',
    props: {
      //图片属性数组
      value: Array,
      //最大上传图片数量
      maxCount:{
        type:Number,
        default:5
      }
    },
    data() {
      return {
        dataObj: {
          policy: '',
          signature: '',
          key: '',
          ossaccessKeyId: '',
          dir: '',
          host: ''
        },
        action:'',
        dialogVisible: false,
        dialogImageUrl:null,
        fileList:[],
        first:true,
        uuid:'',
        fileUrl:[]
      };
    },
    watch: {
      value: {
        handler(newVal, oldVal) {
            console.info("我被执行了")
          if(this.value != null && this.value !== ''){
            let fileList=[];
            for(let i=0;i<newVal.length;i++){
              fileList.push({name:newVal[i].substr(newVal[i].lastIndexOf("/") + 1),url:newVal[i]});
            }
            this.fileList = fileList
            this.fileUrl=this.value
          }
        },
//        immediate: true
      },
    },
    methods: {
        //
        searchImg(url){
          this.dialogVisible = true
          this.dialogImageUrl = url
        },
        //图片的拖动事件
        draggEnd(){
            this.$emit('input', this.fileUrl)
        },
        //图片删除事件
        deleImg(url,index){
            if(this.fileUrl.length > 0){
                this.fileUrl.splice(index,1)
            }
            this.$emit('input', this.fileUrl)
        },
      emitInput(fileList) {
        console.log('111')
        console.log(fileList)
        for(let i=0;i<fileList.length;i++){
          console.log(i)
          if (i+1 == fileList.length){
            fileList[i].name = this.uuid+fileList[i].name
          }
        }
        this.fileUrl.push(this.dataObj.host + '/' + this.dataObj.dir + '/'+ fileList[fileList.length-1].name)
        this.$emit('input', this.fileUrl)
      },
      handleRemove(file, fileList) {
        let value=[];
        for(let i=0;i<fileList.length;i++){
          value.push(fileList[i].url);
        }
        this.$emit('input', value)
      },
      handlePreview(file) {
        console.log('333')
        this.dialogVisible = true;
        this.dialogImageUrl=file.url;
      },
      beforeUpload(file) {
        console.log('444')
        let _self = this;
        _self.uuid = utils.getUUID()
        return new Promise((resolve, reject) => {
          policy().then(response => {
              console.info("上传图片")
              console.info(response)
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessKeyId;
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.key = response.data.dir+'/'+_self.uuid+"${filename}";
            _self.dataObj.host = response.data.host;
            _self.action = response.data.action;
            resolve(true)
          }).catch(err => {
            console.log(err)
            reject(false)
          })
        })
      },
      handleUploadSuccess(res, file,fileList) {
        console.log('5555')
        this.fileList = fileList;
        this.emitInput(this.fileList);
        console.log(fileList)
        console.log("*******************")
        console.log(this.fileList)
      },
      handleExceed(files, fileList) {
        this.$message({
          message: '最多只能上传'+this.maxCount+'张图片',
          type: 'warning',
          duration:1000
        });
      },
    }
  }
</script>
<style>
    .divbox{
      
      display: flex;
        flex-direction: row;
        align-self:center;
        flex-wrap: wrap;
    }
    .imgbox{
      padding: 0;
      margin: 0;
        display: flex;
        flex-direction: row;
        align-self:center;
        flex-wrap: wrap;
    }
    .imgboxInner{
      margin-right: 10px;
      width: 150px;
      height: 150px;
      margin-bottom: 10px;
      border: solid 1px rgb(167, 167, 167);
      border-radius: 10px;
      position: relative;
    }
    .imgboxInner img{
      border-radius: 10px;
    }
    .imgSearch{
      position:absolute;
      bottom: 5px;
      left: 5px;
    }
    .imgDelete{
      position:absolute;
      bottom: 5px;
      right: 5px;
    }
</style>


